package com.aiju.cash.ui.main.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.aiju.cash.R;
import com.aiju.cash.ui.multiple_shops.widget.MenuLinearLayout;

/**
 * 作者：流冥 on 2017/6/15 15:48
 * 说明：搜索框
 */

public class BlotterSearchBar extends LinearLayout {
    private EditText edi;
    private ImageView clear;

    public BlotterSearchBar(Context context) {
        super(context);
        initView(context);
    }

    public BlotterSearchBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public BlotterSearchBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    void initView(Context mContext) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_serch_bar, null);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, MenuLinearLayout.dip2px(mContext, 50));
        view.setLayoutParams(layoutParams);
        edi = (EditText) view.findViewById(R.id.edi);
        clear = (ImageView) view.findViewById(R.id.clear);
        setClicklistner();
        addView(view);


    }

    void setClicklistner() {
        clear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                clear.setVisibility(GONE);
                edi.setText("");
                mLister.clearIamgeClick();
            }
        });
        edi.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                return false;
            }
        });

        edi.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = s.toString().trim().length();
                if (length > 0) {
                    clear.setVisibility(VISIBLE);
                    mLister.editextWaterValue(s.toString());
                } else {
                    mLister.edutextWaterNoValue();
                    clear.setVisibility(GONE);
                }

            }
        });

    }

    public interface SeachBarListener {
        void editextWaterValue(String value);

        void edutextWaterNoValue();

        void clearIamgeClick();
    }

    private SeachBarListener mLister;

    public void setLIster(SeachBarListener lIster) {
        mLister = lIster;
    }

    public void setEdiHint(String hint) {
        edi.setHint(hint);
    }
}
